package com.jesper.controller;

import com.jesper.model.Property;
import com.jesper.service.PropertyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Controller
@Slf4j
public class PropertyController {
    @Autowired
    PropertyService propertyService;

    @GetMapping("/user/propertyCheck/{itemId}")
    public String propertyManage(@PathVariable Long itemId, Model model) {//属性列表
        List<Property> property = propertyService.getListById(itemId);
        model.addAttribute("itemId", itemId);

        model.addAttribute("property", property);
        return "item/propertyManage";
    }

    @GetMapping("/user/propertyCheck/propertyEdit")
    public String propertyEdit(Long id, Long itemId, Model model) {//属性编辑界面填充
        if (id == null) {
            Property property = new Property();
            property.setItemId(itemId);
            model.addAttribute("pro", property);
        } else {

            Property property1 = propertyService.getById(id);
            model.addAttribute("pro", property1);
        }
        return "item/propertyEdit";
    }

    @PostMapping("/user/propertyCheck/propertyEdit")
    public String propertyEditPost(Property property, Model model) {//提交更改
        if (property.getId() == null) {
            propertyService.save(property);
        } else {
            propertyService.updateById(property);
        }
        return "redirect:/user/propertyCheck/" + property.getItemId();
    }

    @GetMapping("/user/propertyCheck/propertyDelete/{id}")
    public String propertyDelete(@PathVariable Long id) {
        Long ItemID = propertyService.getItemId(id);
        propertyService.removeById(id);
        return "redirect:/user/propertyCheck/" + ItemID;
    }

}
